package com.cqupt.tr.component.role;


import com.cqupt.tr.Utils.ObjectJsonTransFunction;
import com.cqupt.tr.bean.BaseMessage;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by root on 2019/2/27.
 */
@Component
public class AjaxAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json; charset=utf-8");
        BaseMessage baseMessage = new BaseMessage();
        baseMessage.setMsg("failed");
        baseMessage.setCode("1");
        httpServletResponse.getWriter().write(ObjectJsonTransFunction.toJsonStr(baseMessage));
    }


}
